﻿Imports System.Xml
Imports System.IO
Imports System.Data
Imports System.Text

Public Class ClsListObservations
    Public xmlWriterx As XmlWriter
    Private m_lstStations As List(Of ClsStation)

    '##########################################################################################################################
    '                       FONCTIONS
    '##########################################################################################################################

    Public Function ExportListObsTOXML() As String
        Try
            Dim output As New StringWriter()
            Dim xsettings As New XmlWriterSettings()
            xsettings.Indent = True
            xsettings.NewLineOnAttributes = True
            xsettings.Encoding = New UTF8Encoding(False)
            Dim XmlWriter As XmlWriter = XmlWriter.Create(output, xsettings)
            XmlWriter.WriteProcessingInstruction("xml", "version=""1.0"" encoding=""utf-8"" standalone=""yes""")
            XmlWriter.WriteStartElement("RELEVES")
            For Each sta As ClsStation In m_lstStations
                XmlWriter = sta.ExportStaToXML(XmlWriter)
            Next
            XmlWriter.WriteEndElement()
            XmlWriter.WriteEndDocument()
            XmlWriter.Flush()
            Return output.ToString()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    '#################################################################################################
    '               CONSTRUCTEURS
    '#################################################################################################
    Public Sub New()
        Me.m_lstStations = New List(Of ClsStation)
    End Sub

    Public Sub New(ByVal lstObservations As List(Of ClsStation))
        Me.m_lstStations = New List(Of ClsStation)
        Me.m_lstStations = lstObservations
    End Sub

    Public Sub New(ByVal sXmlObservationsFile As String)
        Me.m_lstStations = New List(Of ClsStation)
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Try
            'Création du document XML
            m_xmld = New XmlDocument()
            'Chargement du fichier XML
            m_xmld.Load(sXmlObservationsFile)

            'Pour chaque Observation
            m_nodelist = m_xmld.SelectNodes("/RELEVES/RELEVE")
            For Each m_node In m_nodelist
                'Création d'une nouvelle observation et Ajout de l'observation à la liste
                AddOneXMLStation("<RELEVE> " & m_node.InnerXml & "</RELEVE>")
            Next
        Catch ex As Exception
            Throw New Exception("Erreur chargement fichier observations: " & ex.Message)
        End Try

    End Sub

    '#################################################################################################
    '               Accesseurs et Cie
    '#################################################################################################
    Public Property lstStations() As List(Of ClsStation)
        Get
            Return m_lstStations
        End Get
        Set(ByVal value As List(Of ClsStation))
            m_lstStations = value
        End Set
    End Property

    Public Function AddOneObjectStation(ByVal oSta As ClsStation) As Integer
        Try
            m_lstStations.Add(oSta)
            Return m_lstStations.LastIndexOf(oSta)
        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Function AddOneXMLStation(ByVal strXMLSta As String) As Integer
        Try
            Dim oSta As New ClsStation(strXMLSta)
            'Ajout de la station à la liste
            m_lstStations.Add(oSta)
            Return m_lstStations.LastIndexOf(oSta)
        Catch ex As Exception
            Return 0
        End Try
    End Function

End Class